package com.download.cn.download;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.util.List;

/**
 * Created by abc on 2017/6/12.
 */
public class DownLoadService extends Service {

    private DownLoadManager mDownLoadManager;

    private final DownLoadServiceBinder mDownLoadServiceBinder = new DownLoadServiceBinder();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(mDownLoadManager==null){
            mDownLoadManager = new DownLoadManager(this);
        }
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mDownLoadServiceBinder;
    }

    /**
     * (增加一个任务，默认开始执行下载任务)
     *
     * @param taskID 任务号
     * @param url 请求下载的路径
     * @param fileName 文件名
     * @param filepath 下载到本地的路径
     * @return -1 : 文件已存在 ，0 ： 已存在任务列表 ， 1 ： 添加进任务列表
     */
    public int startDownLoadTask(String taskID, String url, String fileName, String filepath){
        if(mDownLoadManager==null){
            mDownLoadManager = new DownLoadManager(this);
        }
        return mDownLoadManager.addTask(taskID,url,fileName,filepath);
    }

    public List<TaskInfo> getTasks(){
        if(mDownLoadManager!=null){
            return mDownLoadManager.getAllTask();
        }
        return null;
    }

    public void startTask(String taskId){
        mDownLoadManager.startTask(taskId);
    }

    public void stopTask(String taskId){
        mDownLoadManager.stopTask(taskId);
    }

    public void startAllTask(){
        mDownLoadManager.startAllTask();
    }

    public void stopAllTask(){
        mDownLoadManager.stopAllTask();
    }

    public void deleteTask(String taskId){
        mDownLoadManager.deleteTask(taskId);
    }

    public DownLoadManager getDownLoadManager(){
        if(mDownLoadManager!=null){
            return mDownLoadManager;
        }
        return null;
    }

    public class DownLoadServiceBinder extends Binder {

        public DownLoadService getService() {
            return DownLoadService.this;
        }



    }

}
